//
//  UserModel.swift
//
//  Created by apple on 2017/4/23.
//  Copyright © 2017年 FancgzFitech. All rights reserved.
//


import Foundation

enum orgTypeEnum:Int {
    /** 云平台 */
    case PM = 0
    /** 供应商 */
    case VENDOR = 1
    /** 客户 */
    case CUSTOMER = 2
    /** 未知 */
    case UNKNOWN = 100
}

/**
 * 用户信息实体
 */
struct UserModel {
    
    /**
     {
         "id": 30,
         "username": "gt",
         "mobile": "#88#234563#",
         "email": "gt#gomro.cn",
         "jobNumber": null,
         "fullName": "广通",
         "departmentId": 6,
         "departmentName": "广通采购部",
         "orgIzaId": 6,
         "orgIzaName": "上海广通工业品科技有限公司",
         "orgType": "CUSTOMER",//PM(0, 云平台),VENDOR(1, 供应商),CUSTOMER(2, 客户)
         "orgTypeName": "客户",
         "qq": null
     }
     */
    
    //用户登录状态key
    private static let k_user_model_is_logined = "user_model_is_logined"
    
    //用户指纹登录key
    private static let k_user_model_is_touchID_logined = "user_model_is_touchID_logined"
    
    //用户信息
    private static let k_user_model_dic_data = "user_model_dic_data"

    //融云Token(默认永久有效)
    static let k_user_rong_cloud_token = "token"

    //0 手机登录
    private static let k_user_login_type = "user_login_type"

    //服务端返回的tooken
    static let k_user_tooken = "access_token"
    
    private static let userDefaults:UserDefaults = UserDefaults.init(suiteName: K_APP_GROUP) ?? UserDefaults.standard
    
    
    //MARK: - class method
    /**
     * 用户是否已登录
     * @return true 已登录
     */
    static var userIsLogined:Bool {
        get {
            let isLogined:Bool? = userDefaults.bool(forKey: k_user_model_is_logined)
            
            //已登录
            if isLogined != nil && isLogined == true {
                if let _:[String:Any] = userDefaults.dictionary(forKey: k_user_model_dic_data) {
                     return true
                }
            }
            
            return false
        }
        set(newVal){
            userDefaults.set(newVal.self, forKey: k_user_model_is_logined)
            userDefaults.synchronize()
        }
    }

    /**
     * 用户登录类型
     * 0 手机登录 1 QQ 2 微信 3 新浪微博
     * @return Int
     */
    static var userLoginType:Int {
        get {
            return userDefaults.integer(forKey: k_user_login_type)
        }
        set(newVal){
            userDefaults.set(newVal.self, forKey: k_user_login_type)
            userDefaults.synchronize()
        }
    }

    /**
     * 服务端返回的登录凭据tooken
     * @return String
     */
    static var userTooken:String? {
        get {
            guard let _uk = userDefaults.string(forKey: k_user_tooken) else { return "" }
            return _uk
        }
        set(newVal){
            userDefaults.set(newVal, forKey: k_user_tooken)
            userDefaults.synchronize()
        }
    }
    
    /**
     * 用户登录保存信息
     * @return [String:Any] / nil
     */
    static var userDicData:[String:Any]? {
        get{
            if self.userIsLogined {
                if let _dic:[String:Any] = userDefaults.dictionary(forKey: k_user_model_dic_data) {
                    return _dic
                }
            }
        
            return nil
        }
        set(newVal){
            //过滤掉字典中的Null
            if newVal != nil {
                var _newDic:[String:Any] = [String:Any]()
                for (k,v) in newVal! {
                    if v is NSNull {
                        _newDic[k] = ""
                    }
                    else {
                        _newDic[k] = v
                    }
                }
                
                userDefaults.set(_newDic, forKey: k_user_model_dic_data)
            }
            else {
                userDefaults.set(newVal, forKey: k_user_model_dic_data)
            }
            
            userDefaults.synchronize()
        }
    }

    /** 当前用户的融云Token */
    static var userRongCloudToken:String? {
        get{
            return userDefaults.string(forKey: k_user_rong_cloud_token)
        }
        set(newVal){
            userDefaults.set(newVal, forKey: k_user_rong_cloud_token)
            userDefaults.synchronize()
        }
    }

    /** 当前用户编号(唯一标识) */
    static var userId:Int? {
        get{
            if userIsLogined,let _id = self.userDicData?["id"] as? Int {
                return _id
            }
            return 0
        }
    }

    /** 当前用户图像(暂无) */
    static var userPicture:String? {
        get{
            if userIsLogined {
                return self.userDicData?["avatar"] as? String
            }
            return ""
        }
    }

    /** 当前用户手机号 */
    static var userMobile:String? {
        get{
            if userIsLogined {
                if self.userDicData!.keys.contains("mobile") {
                    var m = String.init(format:"%@",self.userDicData?["mobile"] as! CVarArg)
                    m = m.replacingOccurrences(of: "#", with: "")
                    return m
                }
                else if self.userDicData!.keys.contains("cellphone") {
                    return String.init(format:"%@",self.userDicData?["cellphone"] as! CVarArg).replacingOccurrences(of: "#", with: "")
                }
                return ""
            }
            return ""
        }
    }

    /** 当前用户昵称(暂无) */
    static var userNickName:String? {
        get{
            if userIsLogined {
                return self.userDicData?["nickname"] as? String
            }

            return ""
        }
    }

    /** 当前用户邮箱 */
    static var userEmail:String? {
        get{
            if userIsLogined {
                var e = String.init(format:"%@",self.userDicData?["email"] as! CVarArg)
                e = e.replacingOccurrences(of: "#", with: "@")
                return e
            }

            return ""
        }
    }

    /** 当前用户名 */
    static var userFulleName:String? {
        get{
            if userIsLogined {
                return String.init(format:"%@",self.userDicData?["fullName"] as! CVarArg)
            }

            return ""
        }
    }
    
    /** username */
    static var userName:String? {
        get{
            if userIsLogined {
                return String.init(format:"%@",self.userDicData?["username"] as! CVarArg)
            }

            return ""
        }
    }
    
    /** 当前用户工号 */
    static var userJobNumber:String? {
        get{
            if userIsLogined {
                return String.init(format:"%@",self.userDicData?["jobNumber"] as! CVarArg)
            }

            return ""
        }
    }
    
    /** 当前用户部门编号 */
    static var userDepartmentId:String? {
        get{
            if userIsLogined {
                return String.init(format:"%@",self.userDicData?["departmentId"] as! CVarArg)
            }

            return ""
        }
    }
    
    /** 当前用户部门名称 */
    static var userDepartmentName:String? {
        get{
            if userIsLogined {
                if (self.userDicData?.keys.contains("departmentName"))! {
                   return String.init(format:"%@",self.userDicData?["departmentName"] as! CVarArg)
                }
                else if (self.userDicData?.keys.contains("deptName"))! {
                   return String.init(format:"%@",self.userDicData?["deptName"] as! CVarArg)
                }
                return ""
            }

            return ""
        }
    }
    
    /** orgIzaId */
    static var userOrgIzaId:String? {
        get{
            if userIsLogined {
                if (self.userDicData?.keys.contains("organizationId"))! {
                    return String.init(format:"%@",self.userDicData?["organizationId"] as! CVarArg)
                }
                else if (self.userDicData?.keys.contains("orgIzaId"))! {
                    return String.init(format:"%@",self.userDicData?["orgIzaId"] as! CVarArg)
                }
                else if (self.userDicData?.keys.contains("orgId"))! {
                    return String.init(format:"%@",self.userDicData?["orgId"] as! CVarArg)
                }
                return ""
            }

            return ""
        }
    }
    
    /** 公司名 orgIzaName */
    static var userOrgIzaName:String? {
        get{
            if userIsLogined {
                if self.userDicData!.keys.contains("orgIzaName") {
                    return String.init(format:"%@",self.userDicData?["orgIzaName"] as! CVarArg)
                }
                else if self.userDicData!.keys.contains("organizationName") {
                    return String.init(format:"%@",self.userDicData?["organizationName"] as! CVarArg)
                }
                else if self.userDicData!.keys.contains("orgName") {
                    return String.init(format:"%@",self.userDicData?["orgName"] as! CVarArg)
                }
                return ""
            }

            return ""
        }
    }
    
    /** orgType */
    static var userOrgType:orgTypeEnum {
        get{
            if userIsLogined {
                switch "\(self.userDicData?["authorities"].map({ return "\($0)" }) ?? "")" {
                case let _temp where _temp.contains("PM"):
                    return .PM
                case let _temp where _temp.contains("VENDOR"):
                    return .VENDOR
                case let _temp where _temp.contains("CUSTOMER"):
                    return .CUSTOMER
                    
                default:
                    return .UNKNOWN
                }
            }

            return .UNKNOWN
        }
    }
    
    /** orgTypeName */
    static var userOrgTypeName:String? {
        get{
            if userIsLogined {
                //return String.init(format:"%@",self.userDicData?["orgTypeName"] as! CVarArg)
                
                switch "\(self.userDicData?["authorities"].map({ return "\($0)" }) ?? "")" {
                case let _temp where _temp.contains("PM"):
                    return "云平台"
                case let _temp where _temp.contains("VENDOR"):
                    return "供应商"
                case let _temp where _temp.contains("CUSTOMER"):
                    return "客户"
                    
                default:
                    return ""
                }
            }

            return ""
        }
    }
    
    /** qq */
    static var userQQ:String? {
        get{
            if userIsLogined {
                return String.init(format:"%@",self.userDicData?["qq"] as! CVarArg)
            }

            return ""
        }
    }

    /**!
     * 是否开启指纹登录
     * true 开启
     */
    static var touchIDLogin:Bool {
        get {
            let _touchIDLogin:Bool? = userDefaults.bool(forKey: k_user_model_is_touchID_logined)
            
            //已开启
            if _touchIDLogin != nil && _touchIDLogin == true {
                return true
            }
            
            return false
        }
        set(newVal){
            userDefaults.set(newVal.self, forKey: k_user_model_is_touchID_logined)
            userDefaults.synchronize()
        }
    }
}
